package com.kly.sc.api.application.controller.user;

import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.Response;
import com.fqgj.common.api.annotations.ParamsValidate;
import com.kly.sc.api.application.constant.ApiRedisKeys;
import com.kly.sc.api.application.controller.user.request.ChangeImagesRequest;
import com.kly.sc.api.application.controller.user.response.ChangeImagesRsp;
import com.kly.sc.api.application.helper.RequestLocalInfo;
import com.kly.sc.api.application.utils.RpcUtils;
import com.kly.user.dto.chatter.request.ChangeImageReq;
import com.kly.user.dto.chatter.response.ImagesDto;
import com.kly.user.enums.ImageOpTypeEnum;
import com.kly.user.enums.ImageType;
import com.kly.user.service.OtherService;
import com.kly.user.service.UserAlbumsContentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 照片上传或修改接口
 * @author linqu
 */
@Slf4j
@RestController
@RequestMapping("/api/images")
public class ImageController {

    @Resource
    UserAlbumsContentService userAlbumsContentService;
    @Resource
    RedisClient redisClient;
    @Resource
    OtherService otherService;

    /**
     * 说明：
     * 1）只支持单张照片、视频的上传或更新操作
     * 2）只对个人相册进行处理，不支持私密照照片处理
     */
    @ResponseBody
    @RequestMapping(value = "/changeImage")
    @ParamsValidate
    public Response changeUserInfo(@RequestBody ChangeImagesRequest changeImagesRequest) {
        final String userCode = RequestLocalInfo.getCurrentUser().getUserCode();

        final ChangeImageReq changeImageReq = getChangeImageReq(changeImagesRequest, userCode);
        if (ImageOpTypeEnum.upload.getType().equals(changeImagesRequest.getOpType())) {
            final String imageUrl = redisClient.getString(ApiRedisKeys.USER_UPLOAD_IMAGE_LOCK.getValue(), userCode);
            if (StringUtils.isNotBlank(imageUrl) && imageUrl.equals(changeImageReq.getImageUrl())) {
                return Response.error("You have uploaded photo!");
            }
        }

        final ImagesDto imagesDto = RpcUtils.getResult(userAlbumsContentService.changeImage(changeImageReq));

        if (Objects.nonNull(imagesDto)) {
            if (ImageOpTypeEnum.upload.getType().equals(changeImagesRequest.getOpType())) {
                redisClient.set(ApiRedisKeys.USER_UPLOAD_IMAGE_LOCK.getValue(), changeImageReq.getImageUrl(), 1, userCode);
            }

            final ChangeImagesRsp imagesRsp = ChangeImagesRsp.builder().imageCode(imagesDto.getImageCode())
                    .imageUrl(otherService.getFullImageUrl(imagesDto.getImageUrl())).build();
            return Response.ok(imagesRsp);
        }
        return Response.error("Upload profile photo failed!");

    }

    private ChangeImageReq getChangeImageReq(@RequestBody ChangeImagesRequest changeImagesRequest, String userCode) {
        return ChangeImageReq.builder().imageCode(changeImagesRequest.getImageCode()).imageUrl(changeImagesRequest.getImageUrl())
                    .imageType(ImageType.fromType(changeImagesRequest.getType())).avatarFlag(changeImagesRequest.getAvatarFlag())
                    .opType(ImageOpTypeEnum.fromType(changeImagesRequest.getOpType()))
                    .userCode(userCode).videoLength(changeImagesRequest.getVideoLength()).build();
    }


}
